﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WheelOfCivilization.Logic
{
    enum StateStatus
    {
        NoState      = 0x000,
        FailedToSave = 0x001,
        SavedToState = 0x002,
        FailedToLoad = 0x003,
        LoadedState  = 0x004,
    }
    class StateManager
    {
        // <property>
        // StateManager _instance
        // The instance of the state manager. The idea here is to have a single instance of the state manager 
        // So we can prevent any accidental loading or saving.  
        // </property>
        private static StateManager _instance;
        public static StateManager Instance
        {
            get
            {
                return _instance;
            }
        }

        // <property>
        // string file.
        // The file name for our latest "state".
        // </property>
        private string _file;
        public string File
        {
            get
            {
                return _file;
            }
        }

        // <property>
        // enum StateStatus state
        // the current state of the file.
        // </property>
        private StateStatus _stateIndicator;
        public StateStatus StateIndicator
        {
            get
            {
                return _stateIndicator;
            }
        }

        // <property>
        // State state
        // the current state of the file.
        // </property>
        private State _state;
        public State State
        {
            get
            {
                return _state;
            }
        }

        // <property>
        // delegate StateStatusChange.
        // The delegate type for a change in file status.
        //
        // A state has new status when a state (our file) has been loaded, saved, or failed to either. 
        // This is going to be event based so when any piece of code can listen for the change. If this is not 
        // necessary, then let me (IanSchweer) know and I will change the logic
        // </property>
        public delegate void StateStatusChange(StateStatus status, State state);
        public StateStatusChange OnStateChange;

        // constructor
        // The constructor of the StateManager class.
        // 
        // @param string file
        //      For now the file has a default of nothing because it is still in development.
        public StateManager(string file = "")
        {
            this._file = file;
            this._stateIndicator = StateStatus.NoState;
            _instance = this;
        }

        // attemptLoadState
        // Load state will attempt to load the current state of the game
        // from the file.
        public void attemptLoadState()
        {
            // @todo Implement loading state from file.
            this._state = new State
            {
                gold = new List<Microsoft.Xna.Framework.Rectangle>(),
                center = new List<Microsoft.Xna.Framework.Rectangle>(),
                stone = new List<Microsoft.Xna.Framework.Rectangle>(),
                wood = new List<Microsoft.Xna.Framework.Rectangle>(),
                ground = new State.Ground
                {
                    noneSelectedMoveableGround = new List<Microsoft.Xna.Framework.Rectangle>(),
                    noneSelectedNoneMoveableGround = new List<Microsoft.Xna.Framework.Rectangle>(),
                    selectedMoveableGround = new List<Microsoft.Xna.Framework.Rectangle>()
                }
            };

            this._stateIndicator = StateStatus.LoadedState;

            // Fire on state change event.
            if (this.OnStateChange != null)
            {
                this.OnStateChange.Invoke(this._stateIndicator, this._state);
            }
        }
    }
}
